home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / AbsString.c next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  3.3 KB  |  135 lines  |  [TEXT/ALFA]

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /*
  30.  * Harvest C
  31.  * 
  32.  * Copyright 1991 Eric W. Sink   All rights reserved.
  33.  * 
  34.  * This file implements operations on abstract strings.
  35.  * 
  36.  */
  37.  
  38. #include "conditcomp.h"
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include "structs.h"
  42.  
  43. #pragma segment AbsStrings
  44.  
  45. #include "AbsString.h"
  46.  
  47. /*
  48.  * An abstract string is a reference to a string in a pool.  The pool is
  49.  * simply a large relocatable region of memory which can be grown. A
  50.  * reference to an individual string in the pool is simply the offset of that
  51.  * string from the start of the pool.
  52.  */
  53.  
  54. struct StringPool {
  55.     EString_t                        Pool;
  56.     unsigned long                   PoolSize;
  57.     unsigned long                   CountStrings;
  58.     unsigned long                   TotalLength;
  59. };
  60.  
  61. StringPoolVia_t
  62. RawStringPool(unsigned long startsize)
  63. {
  64.     StringPoolVia_t                 raw;
  65.     raw = Ealloc(sizeof(StringPool_t));
  66.     if (raw) {
  67.     Via(raw)->PoolSize = startsize;
  68.     Via(raw)->CountStrings = 0;
  69.     Via(raw)->TotalLength = 0;
  70.     Via(raw)->Pool = Ealloc(startsize);
  71.     }
  72.     return raw;
  73. }
  74.  
  75. void
  76. GrowPool(StringPoolVia_t pool)
  77. {
  78. #ifdef OLDMEM
  79.     SetHandleSize((Handle) Via(pool)->Pool, Via(pool)->PoolSize * 1.5);
  80. #else
  81.     char *newPool;
  82.     newPool = (char *) icemalloc((long) (Via(pool)->PoolSize * 1.5));
  83.     memcpy(newPool,Via(pool)->Pool,Via(pool)->PoolSize);
  84.     icefree(Via(pool)->Pool);
  85.     Via(pool)->Pool = (EString_t) newPool;
  86. #endif
  87.     Via(pool)->PoolSize *= 1.5;
  88. }
  89.  
  90. AbsStringID
  91. PutString(StringPoolVia_t pool, char *s)
  92. {
  93.     unsigned long                   slen;
  94.     AbsStringID                     result;
  95.     slen = strlen(s);
  96.     if ((Via(pool)->TotalLength + slen) >= Via(pool)->PoolSize) {
  97.     GrowPool(pool);
  98.     }
  99.     strcpy((char *) &(Via(Via(pool)->Pool)[result = Via(pool)->TotalLength]), s);
  100.     Via(pool)->CountStrings++;
  101.     Via(pool)->TotalLength += (slen + 1);
  102.     return result;
  103. }
  104.  
  105. void
  106. GetAbsString(StringPoolVia_t pool, AbsStringID sid, char *s)
  107. {
  108.     strcpy(s, (char *) &(Via(Via(pool)->Pool)[sid]));
  109. }
  110.  
  111. void
  112. KillString(StringPoolVia_t pool, AbsStringID sid)
  113. {
  114.     Via(Via(pool)->Pool)[sid] = 0;
  115. }
  116.  
  117. int
  118. AbsStringCmp(StringPoolVia_t pool, AbsStringID sid, char *nm)
  119. {
  120.     return strcmp((char *) &(Via(Via(pool)->Pool)[sid]), nm);
  121. }
  122.  
  123. int
  124. AbsStringLen(StringPoolVia_t pool, AbsStringID sid)
  125. {
  126.     return strlen((char *) &(Via(Via(pool)->Pool)[sid]));
  127. }
  128.  
  129. void
  130. KillStringPool(StringPoolVia_t pool)
  131. {
  132.     Efree(Via(pool)->Pool);
  133.     Efree(pool);
  134. }
  135.